package com.cxyd.modules.wms.controller;

import com.cxyd.common.Utils.ExcelUtils;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.Result;
import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.exception.ErrorCode;
import com.cxyd.common.utils.ProcResult;
import com.cxyd.common.validator.AssertUtils;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.AddGroup;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.security.user.SecurityUser;
import com.cxyd.modules.security.user.UserDetail;
import com.cxyd.modules.wms.dto.SpuDTO;
import com.cxyd.modules.wms.dto.StockDTO;
import com.cxyd.modules.wms.excel.SpuExcel;
import com.cxyd.modules.wms.excel.StockExcel;
import com.cxyd.modules.wms.service.StockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库存信息
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@RestController
@RequestMapping("wms/stock")
public class StockController {
    private final StockService stockService;

    public StockController(StockService stockService) {
        this.stockService = stockService;
    }

    @GetMapping("page")
    @RequiresPermissions("wms:stock:page")
    public Result<PageData<StockDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<StockDTO> page = stockService.page(params);

        return new Result<PageData<StockDTO>>().ok(page);
    }

    @GetMapping("reelid/{reelId}")
    //@RequiresPermissions("wms:task:reelid")
    @LogOperation("库存货格号请求")
    public Result contLocateGet(@PathVariable("reelId") String reelId) {
        String contLocateNo = stockService.contLocateGet(reelId);
        if(contLocateNo==null||contLocateNo.isEmpty()){
            return new Result().error("reelid["+reelId+"]没入库");
        }
        return new Result().success(contLocateNo);
    }

    @GetMapping("checkPage")
    @RequiresPermissions("wms:stock:page")
    public Result<PageData<StockDTO>> checkPage(@RequestParam Map<String, Object> params) {
        PageData<StockDTO> page = stockService.checkPage(params);
        return new Result<PageData<StockDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:stock:info")
    public Result<StockDTO> get(@PathVariable("id") Long id) {
        StockDTO data = stockService.get(id);

        return new Result<StockDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:stock:save")
    public Result save(@RequestBody StockDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        stockService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:stock:update")
    public Result update(@RequestBody StockDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        stockService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:stock:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        stockService.delete(ids);

        return new Result();
    }

    @PostMapping("reelid")
    @LogOperation("删除库存")
    public Result deleteReel(@RequestBody Map<String, Object> params) {

        ProcResult procResult = stockService.deleteReel(params);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PostMapping("freeze")
    @LogOperation("冻结/解冻")
    public Result freeze(@RequestBody List<StockDTO> dtos) {

        ProcResult procResult = stockService.stockFreeze(dtos);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());

    }
    @PostMapping("check")
    @LogOperation("盘点创建")
    public Result check(@RequestBody Map<String, Object> params) {
        UserDetail user = SecurityUser.getUser();
        params.put("userId", user.getId());
        ProcResult procResult = stockService.stockCheck(params);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PostMapping("affirm")
    @LogOperation("盘点确认")
    public Result affirm(@RequestBody Map<String, Object> params) {
        UserDetail user = SecurityUser.getUser();
        params.put("userId", user.getId());
        ProcResult procResult = stockService.checkAffirm(params);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PostMapping("merge-call")
    @LogOperation("合料呼叫")
    public Result mergeCall(@RequestBody Map<String, Object> params) {
        UserDetail user = SecurityUser.getUser();
        params.put("userId", user.getId());
        ProcResult procResult = stockService.mergeCall(params);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @GetMapping("export")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<StockDTO> list = stockService.getList(params);
        ExcelUtils.exportExcelToTarget(response, null, list, StockExcel.class);
    }

    @PostMapping("lock")
    @LogOperation("冻结/解冻")
    public Result lock(@RequestBody Map<String, Object> params) {

        ProcResult procResult = stockService.lock(params);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());

    }
}